﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using FitnessCenter.Common.Constants;
using FitnessCenter.Common.Extesions;
using FitnessCenter.DataAccessLayer.DataModels;
using FitnessCenter.DataAccessLayer.Repositories.Interfaces;
using FitnessCenter.WpfApp.Events;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.PubSubEvents;

namespace FitnessCenter.WpfApp.ViewModels
{
    public class LogActivityViewModel : BaseViewModel
    {
        private readonly IActivityLogRepository _activityLogRepository;
        private readonly IActivityRepository _activityRepository;
        private readonly IPersonRepository _personRepository;

        #region Properties

        #region Labels

        private string _returnLabel;
        public string ReturnLabel
        {
            get { return _returnLabel; }
            set
            {
                if (_returnLabel != value)
                {
                    _returnLabel = value;
                    OnPropertyChanged();
                }
            }
        }

        private string _visitorLabel;
        public string VisitorLabel
        {
            get { return _visitorLabel; }
            set
            {
                if (_visitorLabel != value)
                {
                    _visitorLabel = value;
                    OnPropertyChanged();
                }
            }
        }

        private string _activityLabel;
        public string ActivityLabel
        {
            get { return _activityLabel; }
            set
            {
                if (_activityLabel != value)
                {
                    _activityLabel = value;
                    OnPropertyChanged();
                }
            }
        }

        private string _dateLabel;
        public string DateLabel
        {
            get { return _dateLabel; }
            set
            {
                if (_dateLabel != value)
                {
                    _dateLabel = value;
                    OnPropertyChanged();
                }
            }
        }

        private string _saveLabel;
        public string SaveLabel
        {
            get { return _saveLabel; }
            set
            {
                if (_saveLabel != value)
                {
                    _saveLabel = value;
                    OnPropertyChanged();
                }
            }
        }
        #endregion

        private ObservableCollection<Activity> _activities;
        public ObservableCollection<Activity> Activities
        {
            get { return _activities; }
            set
            {
                if (_activities != value)
                {
                    _activities = value;
                    OnPropertyChanged();
                }
            }
        }

        private Activity _selectedActivity;
        public Activity SelectedActivity
        {
            get { return _selectedActivity; }
            set
            {
                if (_selectedActivity != value)
                {
                    _selectedActivity = value;

                    if (_selectedActivity != null)
                        Log.ActivityID = _selectedActivity.ActivityID;

                    OnPropertyChanged();
                }
            }
        }

        private ObservableCollection<Person> _visitors;
        public ObservableCollection<Person> Visitors
        {
            get { return _visitors; }
            set
            {
                if (_visitors != value)
                {
                    _visitors = value;
                    OnPropertyChanged();
                }
            }
        }

        private Person _selectedVisitor;
        public Person SelectedVisitor
        {
            get { return _selectedVisitor; }
            set
            {
                _selectedVisitor = value;

                if (_selectedVisitor != null)
                    Log.PersonID = _selectedVisitor.PersonID;

                OnPropertyChanged();
            }
        }

        private DateTime _logDateTime;
        public DateTime LogDateTime
        {
            get { return _logDateTime; }
            set
            {
                if (_logDateTime != value)
                {
                    _logDateTime = value;
                    Log.ActivityDateTime = _logDateTime;

                    OnPropertyChanged();
                }
            }
        }

        private ActivityLog Log { get; set; }
        #endregion

        #region Commands

        public ICommand ReturnToDashboardCommand { get { return new DelegateCommand(OnReturnToDashboard); }}
        public ICommand LogCommand { get { return new DelegateCommand(OnLog); } }
        #endregion

        public LogActivityViewModel
            (
                IEventAggregator eventAggregator,
                IActivityLogRepository activityLogRepository,
                IActivityRepository activityRepository,
                IPersonRepository personRepository
            ) : base(eventAggregator)
        {
            _activityLogRepository = activityLogRepository;
            _activityRepository = activityRepository;
            _personRepository = personRepository;

            Load();
        }

        #region Methods

        private void Load()
        {
            SetLabels();

            Log = new ActivityLog();

            Activities = _activityRepository.GetAll().AsObservable();
            Visitors = _personRepository.GetVisitors().AsObservable();
            LogDateTime = DateTime.Now;
        }

        private void SetLabels()
        {
            ReturnLabel = Phrases.ReturnToDashboard;
            VisitorLabel = Phrases.SelectVisitor;
            ActivityLabel = Phrases.SelectActivity;
            DateLabel = Phrases.SelectDate;
            SaveLabel = Phrases.Save;
        }
        #endregion

        #region Command handlers

        private void OnReturnToDashboard()
        {
            EventAggregator.GetEvent<SelectModuleEvent>().Publish(ModuleNames.DashboardModule);
        }

        private void OnLog()
        {
            _activityLogRepository.Save(Log);
            Log = new ActivityLog();
        }
        #endregion
    }
}
